package com.kis.bot.utils.api

import com.alibaba.fastjson.JSON
import com.kis.bot.enums.StringEnums
import com.kis.bot.model.WeChat
import com.squareup.okhttp.FormEncodingBuilder
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import java.net.URLDecoder
import java.net.URLEncoder
import java.util.*

/**
 * Created by wanglong on 16-8-31.
 */
object OperApi {
    fun initWx(wx: WeChat) {
        System.setProperty("jsse.enableSNIExtension", "false")
        val client = OkHttpClient()
        val form = FormEncodingBuilder()
        form.add("BaseRequest", "{Uin:${wx.uin},Sid:${wx.sid},Skey:${wx.sKey},DeviceID:${wx.pass_ticket}")
        println(wx.baseUri)
        println(wx.baseRequest.toString())
        var url = wx.baseUri + "/webwxinit?pass_ticket=${wx.pass_ticket}&r=${Date().getShortTime()}&lang=zh_CN"
        println(url)
        println(form.build())
        val request = Request.Builder()
                .url(url)
                .post(form.build())
                .addHeader("ContentType", "application/json; charset=UTF-8")
                .addHeader("User-agent", StringEnums.USERAGENT.value)
                .build()
        val response = client.newCall(request).execute()
        if (response.isSuccessful) {
            println(response.body().string())
        } else {
            println("服务器错误${response}")
        }
    }
}